package top.tangyh.lamp.care.mapper;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import top.tangyh.basic.base.mapper.SuperMapper;
import top.tangyh.lamp.care.entity.BHl7Yz;
import org.springframework.stereotype.Repository;
import top.tangyh.lamp.care.vo.query.YzQueryVO;

import java.util.Collection;
import java.util.Date;
import java.util.List;

import static top.tangyh.lamp.common.constant.CommonConstant.NO_NUMBER;

/**
 * <p>
 * Mapper 接口
 * 医嘱表包含用药，检验，检查
 * </p>
 *
 * @author zqb
 * @date 2024-07-15 10:48:09
 * @create [2024-07-15 10:48:09] [zqb] [代码生成器生成]
 */
@Repository
public interface BHl7YzMapper extends SuperMapper<BHl7Yz> {

    List<BHl7Yz> selectYzOfTopNewTime(String typeIdent, List<String> hospitalNumbers);

    default List<BHl7Yz> selectListByHospitalNumbersAndType(List<String> hospitalNumbers, String yzType) {
        LambdaQueryWrapper<BHl7Yz> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BHl7Yz::getIsDeleted, 0)
                .eq(BHl7Yz::getTypeIdent, yzType)
                .in(BHl7Yz::getHospitalNumber, hospitalNumbers);
        return this.selectList(queryWrapper);
    }

    default List<BHl7Yz> selectListByHospitalNumberAndType(String hospitalNumbers, String yzType) {
        LambdaQueryWrapper<BHl7Yz> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BHl7Yz::getIsDeleted, 0)
                .eq(BHl7Yz::getTypeIdent, yzType)
                .eq(BHl7Yz::getHospitalNumber, hospitalNumbers);
        return this.selectList(queryWrapper);
    }

    default List<BHl7Yz> selectListByQuery(YzQueryVO queryVO) {
        String patientIdent = queryVO.getPatientIdent();
        String hospitalNumber = queryVO.getHospitalNumber();
        String conditionIdent = queryVO.getConditionIdent();
        String typeIdent = queryVO.getTypeIdent();
        String statusIdent = queryVO.getStatusIdent();
        Long startTime = queryVO.getStartTime();
        Long endTime = queryVO.getEndTime();
        if (StrUtil.isNotEmpty(typeIdent) && typeIdent.equals("9")) {
            LambdaQueryWrapper<BHl7Yz> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(StrUtil.isNotBlank(patientIdent), BHl7Yz::getPatientIdent, patientIdent)
                    .eq(StrUtil.isNotBlank(hospitalNumber), BHl7Yz::getHospitalNumber, hospitalNumber)
                    .eq(StrUtil.isNotBlank(conditionIdent), BHl7Yz::getConditionIdent, conditionIdent)
                    .eq(StrUtil.isNotBlank(typeIdent), BHl7Yz::getTypeIdent, typeIdent)
                    .eq(StrUtil.isNotBlank(statusIdent), BHl7Yz::getStatusIdent, statusIdent).and(wapper -> {
                        wapper.or(wapper1 -> {
                            wapper1.and(wapper11 -> {
                                wapper11.isNotNull(BHl7Yz::getEndTime).ge(startTime != null, BHl7Yz::getEndTime, DateUtil.formatDateTime(DateUtil.date(startTime)))
                                        .le(endTime != null, BHl7Yz::getEndTime, DateUtil.formatDateTime(DateUtil.date(endTime)));
                            });
                        });
                        wapper.or(wapper2 -> {
                            wapper2.and(wapper22 -> {
                                wapper22.isNull(BHl7Yz::getEndTime).ge(startTime != null, BHl7Yz::getOrderTime, DateUtil.formatDateTime(DateUtil.date(startTime)))
                                        .le(endTime != null, BHl7Yz::getOrderTime, DateUtil.formatDateTime(DateUtil.date(endTime)));
                            });
                        });
                    });
            return this.selectList(queryWrapper);
        } else {
            LambdaQueryWrapper<BHl7Yz> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(StrUtil.isNotBlank(patientIdent), BHl7Yz::getPatientIdent, patientIdent)
                    .eq(StrUtil.isNotBlank(hospitalNumber), BHl7Yz::getHospitalNumber, hospitalNumber)
                    .eq(StrUtil.isNotBlank(conditionIdent), BHl7Yz::getConditionIdent, conditionIdent)
                    .eq(StrUtil.isNotBlank(typeIdent), BHl7Yz::getTypeIdent, typeIdent)
                    .eq(StrUtil.isNotBlank(statusIdent), BHl7Yz::getStatusIdent, statusIdent)
                    .gt(startTime != null, BHl7Yz::getOrderTime, DateUtil.formatDateTime(DateUtil.date(startTime)))
                    .lt(endTime != null, BHl7Yz::getOrderTime, DateUtil.formatDateTime(DateUtil.date(endTime)));
            return this.selectList(queryWrapper);
        }
    }



    /**
     * 根据住院号查询手术医嘱
     *
     * @param numList
     * @param yzType
     * @return
     */
    default List<BHl7Yz> selectOperasByHospNumListAndType(Collection<String> numList, String yzType) {
        LambdaQueryWrapper<BHl7Yz> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BHl7Yz::getIsDeleted, NO_NUMBER)
                .eq(BHl7Yz::getTypeIdent, yzType)
                .in(BHl7Yz::getHospitalNumber, numList);
        return this.selectList(queryWrapper);
    }

    /**
     * 根据住院号和时间查询手术医嘱数量
     *
     * @param numList
     * @param yzType
     * @return
     */
    default long selectOperasByHospNumListAndTypeAndTime(Collection<String> numList, String yzType, Date startTime, Date endTime) {
        LambdaQueryWrapper<BHl7Yz> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BHl7Yz::getIsDeleted, NO_NUMBER)
                .eq(BHl7Yz::getTypeIdent, yzType)
                .between(BHl7Yz::getPlanTime,startTime,endTime)
                .in(BHl7Yz::getHospitalNumber, numList);
        return this.selectCount(queryWrapper);
    }
}


